package cn.iocoder.yudao.module.wine.controller.admin.supplyorderitem;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wine.controller.admin.supplyorderitem.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplyorderitem.SupplyOrderItemDO;
import cn.iocoder.yudao.module.wine.service.supplyorderitem.SupplyOrderItemService;

@Tag(name = "管理后台 - 订单项信息")
@RestController
@RequestMapping("/wine/supply-order-item")
@Validated
public class SupplyOrderItemController {

    @Resource
    private SupplyOrderItemService supplyOrderItemService;

    @PostMapping("/create")
    @Operation(summary = "创建订单项信息")
    @PreAuthorize("@ss.hasPermission('wine:supply-order-item:create')")
    public CommonResult<Long> createSupplyOrderItem(@Valid @RequestBody SupplyOrderItemSaveReqVO createReqVO) {
        return success(supplyOrderItemService.createSupplyOrderItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新订单项信息")
    @PreAuthorize("@ss.hasPermission('wine:supply-order-item:update')")
    public CommonResult<Boolean> updateSupplyOrderItem(@Valid @RequestBody SupplyOrderItemSaveReqVO updateReqVO) {
        supplyOrderItemService.updateSupplyOrderItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除订单项信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wine:supply-order-item:delete')")
    public CommonResult<Boolean> deleteSupplyOrderItem(@RequestParam("id") Long id) {
        supplyOrderItemService.deleteSupplyOrderItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得订单项信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wine:supply-order-item:query')")
    public CommonResult<SupplyOrderItemRespVO> getSupplyOrderItem(@RequestParam("id") Long id) {
        SupplyOrderItemDO supplyOrderItem = supplyOrderItemService.getSupplyOrderItem(id);
        return success(BeanUtils.toBean(supplyOrderItem, SupplyOrderItemRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得订单项信息分页")
    @PreAuthorize("@ss.hasPermission('wine:supply-order-item:query')")
    public CommonResult<PageResult<SupplyOrderItemRespVO>> getSupplyOrderItemPage(@Valid SupplyOrderItemPageReqVO pageReqVO) {
        PageResult<SupplyOrderItemDO> pageResult = supplyOrderItemService.getSupplyOrderItemPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SupplyOrderItemRespVO.class));
    }

    /**
     * 根据订单ID获取订单项信息列表，含供求基本信息
     * @param orderId
     * @return
     */
    @GetMapping("/list")
    @Operation(summary = "获得订单项信息列表")
    public CommonResult<List<SupplyOrderItemRespVO>> getSupplyOrderItemList(@RequestParam("orderId") Long orderId) {
        List<SupplyOrderItemRespVO> list = supplyOrderItemService.getAdminSupplyOrderItemList(orderId);
        return success(list);
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出订单项信息 Excel")
    @PreAuthorize("@ss.hasPermission('wine:supply-order-item:export')")
    @OperateLog(type = EXPORT)
    public void exportSupplyOrderItemExcel(@Valid SupplyOrderItemPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SupplyOrderItemDO> list = supplyOrderItemService.getSupplyOrderItemPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "订单项信息.xls", "数据", SupplyOrderItemRespVO.class,
                        BeanUtils.toBean(list, SupplyOrderItemRespVO.class));
    }

}